package myplaydog.qf.com.yimixianproject.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import myplaydog.qf.com.yimixianproject.R;
import myplaydog.qf.com.yimixianproject.adapter.KindContentAdapter;
import myplaydog.qf.com.yimixianproject.adapter.KindNavigationAdapter;
import myplaydog.qf.com.yimixianproject.bean.KindContentEnitiy;
import myplaydog.qf.com.yimixianproject.bean.KindNavigationEntity;
import myplaydog.qf.com.yimixianproject.ui.KindItemActivity;
import myplaydog.qf.com.yimixianproject.url.AppNetConfig;

/**
 * 分类
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_kind)
public class KindFragment extends BaseFragment {

    @ViewInject(R.id.fragment_kind_tvTitle)
    private TextView tvTitle;
    @ViewInject(R.id.fragment_kind_ivicon)
    private ImageView ivicon;
    @ViewInject(R.id.fragment_kind_search)
    private ImageView search;
    @ViewInject(R.id.fragment_kind_navigation)
    private ListView navigation;
    @ViewInject(R.id.fragment_kind_ptrListView)
    private PullToRefreshListView ptrListView;

    private KindNavigationAdapter navigationAdapter;
    private KindContentAdapter contentAdapter;

    private List<KindContentEnitiy.GoodsBean> mList;
    private String path =AppNetConfig.URL_KID_GUOCAN;
    private static final int [] pathNum ={51,55,99,52,104,193};
    private int number =0;

    public KindFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navigationAdapter= new KindNavigationAdapter(getContext());
        contentAdapter =new KindContentAdapter(getContext());
        mList =new ArrayList<>();
        loadData();
        loadData1(path);
    }

    private void loadData() {
        RequestParams params =new RequestParams(AppNetConfig.URL_KID_TAB);
        x.http().get(params, new Callback.CacheCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson gson =new Gson();
                KindNavigationEntity entity =gson.fromJson(result,KindNavigationEntity.class);
                navigationAdapter.addAll(entity.getCategories());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("---------","error");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }
    private void loadData1(String path) {

        RequestParams params =new RequestParams(path);
        x.http().get(params, new Callback.CacheCallback<String>() {

            @Override
            public void onSuccess(String result) {
                contentAdapter.clear();
                Gson gson =new Gson();
                KindContentEnitiy entity =gson.fromJson(result,KindContentEnitiy.class);
                contentAdapter.addAll(entity.getGoods());
                mList.addAll(entity.getGoods());
                ptrListView.onRefreshComplete();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = x.view().inject(this, inflater, container);
        navigation.setAdapter(navigationAdapter);
        ptrListView.setAdapter(contentAdapter);
        initPtrLv();
        return view;
    }

    private void initPtrLv() {
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData1(path);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                navigation.getChildAt(number%pathNum.length).setBackgroundColor(Color.argb(00,00,00,00));
                path =String.format(AppNetConfig.URL_KID_CONTENT,pathNum[++number%pathNum.length]);
                loadData1(path);
                navigation.getChildAt(number%pathNum.length).setBackgroundColor(Color.YELLOW);
                ptrListView.getRefreshableView().setSelection(0);//没有特效，直接返回顶部
                ptrListView.getRefreshableView().setSelectionAfterHeaderView();
            }
        });
    }

    //ListView的item点击事件注解操作
    @Event(type = AdapterView.OnItemClickListener.class, value = R.id.fragment_kind_navigation)
    private void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "点击了第" + position + "个item", Toast.LENGTH_LONG).show();
        path =String.format(AppNetConfig.URL_KID_CONTENT,pathNum[position]);
       parent.getChildAt(position).setBackgroundColor(Color.YELLOW);
        parent.getChildAt(number%pathNum.length).setBackgroundColor(Color.argb(00,00,00,00));
        number =position;
        ptrListView.getRefreshableView().setSelection(0);//没有特效，直接返回顶部
        ptrListView.getRefreshableView().setSelectionAfterHeaderView();
        loadData1(path);

    }
    //ListView的item点击事件注解操作
    @Event(type = AdapterView.OnItemClickListener.class, value = R.id.fragment_kind_ptrListView)
    private void onptrItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "点击了第" + position + "个item", Toast.LENGTH_LONG).show();
        Intent intent =new Intent(getContext(),KindItemActivity.class);
        intent.putExtra("id",mList.get(position).getId());
        intent.putExtra("str",mList.get(position).getPromotion_img());
        Log.e("---------",mList.get(position).getId()+"");
        Log.e("----3343-----",mList.get(position).getPromotion_img()+"1111");
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.activity_right_open,0);

    }

}
